package com.atguigu.vod.service.impl;

import com.aliyun.vod.upload.UploadVideo;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import com.atguigu.serviceBase.exception.GuliException;
import com.atguigu.vod.config.VodConfig;
import com.atguigu.vod.service.VodService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author Summerday
 */

@Service
public class VodServiceImpl implements VodService {

    @Autowired
    private UploadVideo uploader;

    @Autowired
    private VodConfig vodConfig;

    @Autowired
    private DeleteVideoRequest deleteVideoRequest;

    @Autowired
    private GetVideoPlayAuthRequest getVideoPlayAuthRequest;

    @Autowired
    private DefaultAcsClient defaultAcsClient;

    @Override
    public String uploadVideo(MultipartFile file) {


        InputStream inputStream = null;
        try {
            String fileName = file.getOriginalFilename();
            assert fileName != null;
            String title = fileName.substring(0, fileName.lastIndexOf("."));
            inputStream = file.getInputStream();
            /**
             * title:上传之后视频的显示名称
             * fileName:上传文件的原始名称
             * inputStream:上传文件输入流
             */
            UploadStreamRequest request = new UploadStreamRequest(vodConfig.getAccessKeyId(), vodConfig.getAccessKeySecret(), title, fileName, inputStream);
            //UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);
            System.out.println("RequestId ==>" + response.getRequestId());  //请求视频点播服务的请求ID
            String videoId = null;
            if (response.isSuccess()) {
                System.out.println("VideoId ==>" + response.getVideoId());
                videoId = response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                System.out.println("VideoId ==>" + response.getVideoId());
                System.out.println("ErrorCode ==>" + response.getCode());
                System.out.println("ErrorMessage ==>" + response.getMessage());
                videoId = response.getVideoId();
            }
            return videoId;

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void removeVideo(String videoSourceId) {
        try {
            deleteVideoRequest.setVideoIds(videoSourceId);
            defaultAcsClient.getAcsResponse(deleteVideoRequest);
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
            throw new GuliException("删除视频失败");
        }
    }

    @Override
    public void removeVideoList(List<String> videoIdList) {
        try {
            String ids = StringUtils.join(videoIdList.toArray(), ',');
            deleteVideoRequest.setVideoIds(ids);
            defaultAcsClient.getAcsResponse(deleteVideoRequest);
        } catch (Exception e) {
            System.out.print("ErrorMessage = " + e.getLocalizedMessage());
            throw new GuliException("删除视频失败");
        }
    }

    @Override
    public String getPlayAuth(String id) {
        try {
            getVideoPlayAuthRequest.setVideoId(id);
            GetVideoPlayAuthResponse response = defaultAcsClient.getAcsResponse(getVideoPlayAuthRequest);
            return response.getPlayAuth();
        } catch (ClientException e) {
            e.printStackTrace();
            return null;
        }
    }
}
